package com.sure.yyr.security.login;

import com.sure.yyr.common.Result;
import com.sure.yyr.common.TokenUtils;
import com.sure.yyr.security.user.UserDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/security/login")
public class LoginAPI {

    @Resource
    private LoginService loginService;

    @PostMapping("")
    public Result login(@RequestBody LoginDto dto){

        boolean ok = loginService.checkLogin(dto);

        if(ok){
            //生成一个登录令牌
            String token = TokenUtils.loginSign(dto.getU_id(),dto.getU_pwd());
            return Result.success((Object)token);//想客户端发送成功信号，并发送令牌

        }

        return Result.fail(Result.ERR_CODE_BUSINESS, "账号或密码错误！");

    }

    /**
     * 注册用户
     * @param dto
     * @return
     */
    @PostMapping("/regist")
    public Result registUser(@RequestBody LoginDto dto){
        loginService.registUser(dto);
        return Result.success();
    }
}
